<template>
	<!-- start 多选 -->
	<form-select-box v-if="multiple" :params="params" :option="option"></form-select-box>
	<!-- end 多选 -->
	
	<!-- start 单选 -->
	<vxe-select
		v-else
		v-model="selectValue"
		placeholder="请选择"
		:option-props="optionProps"
		:options="options"
		:clearable="clearable"
		:multiple="multiple"
		:disabled="disabled"
		:transfer="transfer"
		@change="setVal"
	>
	</vxe-select>
	<!-- end 单选 -->
</template>

<script>
	import FormSelectBox from './FormSelectBox';

	export default{
		name: 'FormSelect',
		components: {
			'form-select-box': FormSelectBox,
		},
		props: {
			params: Object,
			option: Object
		},
		data() {
			return {
				selectValue: null,
				multiple: false,
				disabled: false,
				clearable: false,
				transfer: true,
				options: [],
				optionProps: { value: 'value', label: 'label' },
			}
		},
		created() {
			let row = this.params.row;
			let column = this.params.column;
			let val = row[column.property];
			if (this.option.props) {
				if (this.option.props.multiple != undefined) {
					this.multiple = this.option.props.multiple;
				}
				if (this.option.props.disabled != undefined) {
					this.disabled = this.option.props.disabled;
				}
			}

			let cdValue = this.params.$table.collectColumn[0].own.cdValue;
			let colDriver = this.params.$table.collectColumn[0].own.colDriver;
			if(cdValue && colDriver){
				let cdList = [];
				column.property;
				let count = 0;
				for(let i=0;i<cdValue.length;i++){
					if(cdValue[i].COL == column.property){
						cdValue[i].name = cdValue[i].ENUM_VALUE;
						cdValue[i].value = cdValue[i].ENUM_VALUE;
						cdList.push(cdValue[i]);
					}
				}
				let driverCol = "";
				for(let i = 0;i<colDriver.length;i++){
					if(colDriver[i].COL == column.property){
						driverCol = colDriver[i].DCOL;
					}
				}

				if(driverCol){
					let driverValue = row[driverCol];
					let driverArr = driverValue?driverValue.split(","):[];
					let driverNum = [];
					let enumid = [];
					
					for(let i=0;i<cdValue.length;i++){
						if(cdValue[i].COL == driverCol && this.contains(driverArr,cdValue[i].ENUM_VALUE)){
							driverNum.push(cdValue[i].ENUM_ID);
						}
					}

					for(let i = 0;i<colDriver.length;i++){
						if(colDriver[i].DCOL == driverCol && this.contains(driverNum,colDriver[i].ER_ENUMID )){
							enumid.push(colDriver[i].ENUM_ID);
						}
					}

					if(enumid.length){
						let opts = [];
						for(let i = 0;i<cdList.length;i++){
							if(this.contains(enumid,cdList[i].ENUM_ID)){
								opts.push(cdList[i]);
							}
						}
						opts.sort((a, b) => { return a.value.localeCompare(b.value) });			
						this.options = opts;
					}
				}else{
					cdList.sort((a, b) => { return a.value.localeCompare(b.value) });		
					this.options = cdList;
				}

			}else if(this.option.options != undefined) {
				this.options = this.option.options;
			}
			if(this.option.optionProps != undefined) {
				this.optionProps = this.option.optionProps;
			}
			// 单选时为字符串类型，复选时为数组类型
			if(val == null || val == '') {
				this.selectValue = '';
			}else {
				if(this.multiple) {
					this.selectValue = val.split(',');
				}else {
					this.selectValue = val;
				}
			}
			// 多选模式，处理选择项
			if(this.multiple) {
				for(let i=0; i<this.options.length; i++) {
					let valueKey = this.optionProps.value;
					let check = false;
					if(this.selectValue.indexOf(this.options[i][valueKey]) >= 0) {
						check = true;
					}
					this.options[i].check = check;
				}
			}
		},
		methods: {
			toggleCheck(item) {
				this.setVal2();
			},
			setVal2() {
				let value = [];
				let valueKey = this.optionProps.value;
				for(let i=0; i<this.options.length; i++) {
					if(this.options[i].check) {
						value.push(this.options[i][valueKey]);
					}
				}
				let row = this.params.row;
				let column = this.params.column;
				row[column.property] = value.join(',');
				if(this.multiple) {
					this.selectValue = value;
				}else {
					this.selectValue = value.join(',');
				}
				let colDriver = this.params.$table.collectColumn[0].own.colDriver;
				if(colDriver) {
					let drivenColList = this.getDrivenColList(colDriver, column.property);
					for(let i=0; i<drivenColList.length; i++) {
						row[drivenColList[i]] = '';
					}
				}
			},
			setVal({value, e}) {
				let row = this.params.row;
				let column = this.params.column;
				if(value == null || value == '') {
					row[column.property] = '';
				}else {
					if(this.multiple) {
						row[column.property] = value.join(',');
					}else {
						row[column.property] = value;
					}
				}
				let colDriver = this.params.$table.collectColumn[0].own.colDriver;
				if(colDriver) {
					let drivenColList = this.getDrivenColList(colDriver, column.property);
					for(let i=0; i<drivenColList.length; i++) {
						row[drivenColList[i]] = '';
					}
				}
			},
			contains(arr, obj) {
				var i = arr.length;
				while (i--) {
					if (arr[i] === obj) {
						return true;
					}
				}
				return false;
			},
			/**
			 * 递归获取被驱动列字段
			 */
			getDrivenColList(list, dcol) {
				let list2 = [];
				for(let i=0; i<list.length; i++) {
					if(list[i]['DCOL'] == dcol) {
						let col = list[i]['COL'];
						if(list2.indexOf(col) < 0) {
							list2.push(col);
						}
						this.getDrivenColList2(list, list2, col);
					}
				}
				return list2;
			},
			/**
			 * 递归获取被驱动列字段
			 */
			getDrivenColList2(list, list2, dcol) {
				for(let i=0; i<list.length; i++) {
					if(list[i]['DCOL'] == dcol) {
						let col = list[i]['COL'];
						if(list2.indexOf(col) < 0) {
							list2.push(col);
						}
						this.getDrivenColList2(list, list2, col);
					}
				}
			},

		}
	}
</script>